package DataMock;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Iterator;


import po.MessagePO;
import appendix.ResultMessage;

import dataservice.MessageDataService;

public class MessageDataMock extends UnicastRemoteObject implements MessageDataService{
	private static final long serialVersionUID = 1L;
	private ArrayList<MessagePO> arr;
	
	public MessageDataMock(ArrayList<MessagePO> arr) throws RemoteException {
		this.arr=arr;
	}

	@Override
	public ResultMessage<?> finds(long id) throws RemoteException {
		ArrayList<MessagePO> temp=new ArrayList<MessagePO>();
		Iterator it=arr.iterator();
		MessagePO message=null;
		while(it.hasNext()){
			message=(MessagePO)it.next();
			if(message.getID()==id){
				temp.add(message);
			}
		}
		ResultMessage<ArrayList<MessagePO>> rm=new ResultMessage<ArrayList<MessagePO>>(temp);
		return rm;
	}

	@Override
	public void insert(MessagePO po) throws RemoteException {
		arr.add(po);
	}

	@Override
	public void delete(long id) throws RemoteException {
		ArrayList<MessagePO> temp=new ArrayList<MessagePO>();
		Iterator it=arr.iterator();
		MessagePO message=null;
		while(it.hasNext()){
			message=(MessagePO)it.next();
			if(message.getID()!=id){
				temp.add(message);
			}
		}
		arr=temp;
	}

}
